package com.decelo.jpa.controller;

import com.decelo.jpa.entity.sys.User;
import com.decelo.jpa.result.PageResult;
import com.decelo.jpa.result.Result;
import com.decelo.jpa.result.ResultCode;
import com.decelo.jpa.service.UserService;
import com.decelo.jpa.utils.JWTUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@Validated
@RequestMapping(value = "/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/all", method = RequestMethod.POST, produces = "application/json")
    public List<User> queryAll(HttpServletRequest request) {
        List<User> users = userService.queryAll();
        return users;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    public Result add(HttpServletRequest request,@RequestBody @Validated User user) {
        Result result=new Result();
        boolean bool = userService.edit(user);
        if (bool){
            result.setCode(ResultCode.CODE_200.getCode());
            result.setMsg(ResultCode.CODE_200.getMsg());
            result.setObject(true);
        }else{
            result.setCode(ResultCode.CODE_500.getCode());
            result.setMsg(ResultCode.CODE_500.getMsg());
            result.setObject(false);
        }
        return result;
    }

    @RequestMapping(value = "/queryParams", method = RequestMethod.POST, produces = "application/json")
    public Page<User> queryParams(HttpServletRequest request) {
        int pageNumber = 0;
        int pageSize = 10;
        return userService.queryParams(new PageRequest(pageNumber, pageSize, new Sort(new Sort.Order(Sort.Direction.DESC, "id"))));
    }


    @RequestMapping(value = "/queryByEntityManager", method = RequestMethod.POST, produces = "application/json")
    public List<User> queryByEntityManager(HttpServletRequest request) {
        return userService.queryByPersistContext();
    }

    @RequestMapping(value="/queryByName",method=RequestMethod.POST,produces ="application/json")
    public List<User> queryByName(HttpServletRequest request,@RequestParam(value="name")String name){
        return userService.queryByName(name);
    }

    @RequestMapping(value="/queryPage",method=RequestMethod.POST,produces="application/json")
    public Result queryPage(HttpServletRequest request,@RequestParam(value="pageNo")Integer pageNo,@RequestParam(value="pageSize") Integer pageSize,
                            @RequestParam(value="keyword")String keyword){
        Result result=new Result();
        Map<String,Object> params=new HashMap<>();
        params.put("pageNo",pageNo);
        params.put("pageSize",pageSize);
        if (!"".equals(keyword)){
            params.put("keyword",keyword);
        }
        PageResult<User> userpager=new PageResult<User>();
        params.put("pager",userpager);
        result.setObject(userService.queryByparams(params));
        return result;
    }

    @RequestMapping(value="/login",method=RequestMethod.POST,produces="application/json")
    public Result login(@Validated @RequestParam(value="username",required=false)String username,@RequestParam(value="password",required=false)String password){
        return userService.getUser(username,password);
    }
}